//
//  WLGP_PayView.m
//  GuideInternalPurchase
//
//  Created by 王亮 on 2021/1/12.
//  Copyright © 2021 GuideInternalPurchase. All rights reserved.
//

#import "WLGP_PayView.h"
#import "WLCollectionViewDelegate.h"
#import "WLGP_ShopItemCollectionViewCell.h"
#import "WLGP_ImageCollectionViewCell.h"
#import "WLGP_LabelCollectionViewCell.h"
#import "WLGP_BottonView.h"
#import "WLAppleStore.h"

@interface WLGP_PayView () <WLCollectionDelegate,WLGP_ShopItemCollectionViewCellDelegate>

@property(nonatomic,strong) WLCollectionViewDelegate *delegate_wl;
@property(nonatomic,strong) UICollectionView *collectionView;
@property(nonatomic,strong) WLGP_BottonView *otherView;

@end
@implementation WLGP_PayView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self addSubview:self.collectionView];
        [self addSubview:self.otherView];

        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.insets(UIEdgeInsetsMake(0, 0, 30, 0));
        }];
        [self.otherView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_equalTo(self);
            make.height.mas_offset(30);
        }];
        
        [self loadData];
    }
    return self;
}

#pragma mark -- 数据加载
-(void)loadData{
    [SVProgressHUD show_wl];
    [[DLStore singletonMod] getComListSuccess:^(NSArray * _Nonnull products, NSArray * _Nonnull invalidProductIdentifiers) {
        [self loadCollection:products];
        [SVProgressHUD dismiss_wl];
    } failure:^(NSError * _Nonnull error) {
        [self loadCollection:@[SKProduct.new,SKProduct.new]];
        [SVProgressHUD showError_wl:@"操作失败"];
    }];
}
-(void)loadCollection:(NSArray<SKProduct *> *)products{
    self.delegate_wl.viewModel.viewLayer = [NSMutableArray array];
    
    CGFloat Width = [[UIScreen mainScreen] bounds].size.width;
    CGFloat Height = [[UIScreen mainScreen] bounds].size.height;
    {//图片
        WLBaseCollectionViewLayerSection *section = [WLBaseCollectionViewLayerSection new];
        section.type = @"section";
        {
            WLBaseCollectionViewLayerRow *row = [WLBaseCollectionViewLayerRow new];
            row.type = @"row";
            row.viewClass = WLGP_ImageCollectionViewCell.class;
            
            WLGP_ImageCollectionViewCellModel *model = [WLGP_ImageCollectionViewCellModel new];
            
            row.data = model;
            row.cellSize = CGSizeMake(Width, 200);
            [section.item addObject:row];
        }
        [self.delegate_wl.viewModel.viewLayer addObject:section];
    }
    
    {//文本
        WLBaseCollectionViewLayerSection *section = [WLBaseCollectionViewLayerSection new];
        section.type = @"section";
        {
            WLBaseCollectionViewLayerRow *row = [WLBaseCollectionViewLayerRow new];
            row.type = @"row";
            row.viewClass = WLGP_LabelCollectionViewCell.class;
            
            WLGP_LabelCollectionViewCellModel *model = [WLGP_LabelCollectionViewCellModel new];
            
            row.data = model;
            row.cellSize = CGSizeMake(Width, 100);
            [section.item addObject:row];
        }
        [self.delegate_wl.viewModel.viewLayer addObject:section];
    }
    for (SKProduct *product in products) {
        WLBaseCollectionViewLayerSection *section = [WLBaseCollectionViewLayerSection new];
        section.type = @"section";
        {
            WLBaseCollectionViewLayerRow *row = [WLBaseCollectionViewLayerRow new];
            row.type = @"row";
            row.viewClass = WLGP_LabelCollectionViewCell.class;
            
            WLGP_ShopItemCollectionViewCellModel *model = [WLGP_ShopItemCollectionViewCellModel new];
            model.product = product;
            
            row.data = model;
            row.cellSize = CGSizeMake(Width, 100);
            [section.item addObject:row];
        }
        [self.delegate_wl.viewModel.viewLayer addObject:section];
    }
}

#pragma mark -- WLGP_ShopItemCollectionViewCellDelegate 代理
/** 支付成功 */
-(void)wl_pay_sure_end:(Guide_Close_Type)closeType{
    if ([self.delegate respondsToSelector:@selector(wl_closeViewType:)]){
        [self.delegate wl_closeViewType:closeType];
    }
}
/** 支付失败 */
-(void)wl_pay_fail_end:(Guide_Close_Type)closeType{
    if ([self.delegate respondsToSelector:@selector(wl_closeViewType:)]){
        [self.delegate wl_closeViewType:closeType];
    }
}


#pragma mark -- 懒加载
-(UICollectionView *)collectionView{
    if(!_collectionView){
        UICollectionViewFlowLayout *Layout = [UICollectionViewFlowLayout new];
        [Layout setScrollDirection:UICollectionViewScrollDirectionVertical];
                _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:Layout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.delegate = self.delegate_wl;
        _collectionView.dataSource = self.delegate_wl;
    }
    return _collectionView;
}
-(WLCollectionViewDelegate *)delegate_wl{
    if (!_delegate_wl){
        _delegate_wl = [[WLCollectionViewDelegate alloc]initWithDelegate:self];
    }
    return _delegate_wl;
}
-(WLGP_BottonView *)otherView{
    if (!_otherView) {
        _otherView = [[WLGP_BottonView alloc]init];
    }
    return _otherView;;
}
@end
